﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Services;

namespace FSC.HaNamAPApplication
{
    public partial class RequestAjax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static IEnumerable GetDocItems(string docName, string docField, string docOffice)
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);

            var data = from a in db.Documents
                       orderby a.Doc_ID descending
                       select new
                       {
                           ID = a.Doc_ID,
                           Name = a.Name,
                           Field = a.Field.Name,
                           Office = a.Office.Name,
                           FieldId = a.Field_ID,
                           OfficeId = a.Office_ID
                       };
            if (docName.Trim() != "")
                data = from d in data where d.Name.Contains(docName.Trim()) select d;
            if (docField != "-1")
                data = from d in data where d.FieldId == int.Parse(docField) select d;
            if (docOffice != "-1")
                data = from d in data where d.OfficeId == int.Parse(docOffice) select d;

            var result = from d in data
                         select new
                         {
                             ID = d.ID,
                             Name = d.Name,
                             Field = d.Field,
                             Office = d.Office
                         };
            return result;
        }
    }
}
